<?php
set_time_limit(0);
//dl("php_sdl.dll");
dl("php_guichan.dll");

define('APP_TITLE', 'Guichan window');

//if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_TIMER) < 0) {
if(SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0) {
	trigger_error('SDL can not be initialized. Exiting.');
	exit(1);
}

$screen = SDL_SetVideoMode(625, 500, 32, SDL_HWSURFACE);
if(is_null($screen)) {
	trigger_error('Unable to create a new Window with the desired arguments.');
	exit(1);
}

GUICHAN_Init($screen);
$graphics = GUICHAN_NewGraphics($screen);
GUICHAN_SetGraphics($graphics);
$input = GUICHAN_NewInput();
$top = GUICHAN_NewContainer(0,0,$screen['w'],$screen['h']);

//$font = GUICHAN_NewDefaultFont();
//$font = GUICHAN_NewSDLTrueTypeFont("fixedfont.ttf",12);
$font = GUICHAN_NewImageFont("fixedfont.bmp", " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
GUICHAN_SetGlobalFont($font);

SDL_EnableUNICODE(1);
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
SDL_WM_SetCaption(APP_TITLE, APP_TITLE);

$background = SDL_LoadBMP("bg-gift-card.bmp");
if(is_null($background)) {
	trigger_error('Unable to load the specified image.');
	exit(1);
}

GUICHAN_NewLabel($top,'Hello world!',30,30,80,25);
function mousepressed($p) {
    echo "button is pressed\n";
}
$param1 = array();
GUICHAN_NewButton($top,'bbb',30,90,50,25,'mousepressed',$param1);
//GUICHAN_NewTextBox($top,'fffffffffff',80,120,122,111);
//GUICHAN_NewTextField($top,'fdfgdfgdfghd',80,160,80,25);
function keypressed($p) {
    echo "key is pressed\n";
}
$param2 = array();
GUICHAN_NewTextField($top,'',80,190,90,25,'keypressed',$param2);

GUICHAN_Test($screen, $graphics, $top, 10, 20);

//$color = SDL_MapRGB($screen['format'], 200, 100, 20);
//SDL_FillRect($screen, array('x' => 10,'y' => 20,'w' => 300,'h' => 200), $color);
//SDL_Flip($screen);


function time1($p) {
    echo "time1 is figered\n";
    var_dump($p);
}
$param2 = array();
//$t = sdl_addtimer(1000,'time1',$param2);

$quit = false;
while(!$quit) {
    if (SDL_WaitEvent(&$event)) {
    //if (SDL_PollEvent(&$event)) {
        switch($event['type']) {
    		case SDL_KEYDOWN:
                if ($event['key']['keysym']['sym'] == SDLK_ESCAPE) {
                    $quit = true;
                } elseif ($event['key']['keysym']['sym'] == SDLK_f) {
                    if ($event['key']['keysym']['mod'] & KMOD_CTRL)
                    {
                        // Works with X11 only
                        SDL_WM_ToggleFullScreen($screen);
                    }
                } elseif ($event['key']['keysym']['sym'] == SDLK_9) {
                  SDL_RemoveTimer($t);
                }
    		break;
    		case SDL_MOUSEBUTTONDOWN:
    		break;
    		case SDL_QUIT:
    			$quit = true;
    		break;
    	}

        //SDL_PushEvent($event);
        guichan_pushinput($input, $event);
        
        SDL_BlitSurface($background, null, $screen, null);
        guichan_logic();
        guichan_draw();
        SDL_flip($screen);
    }

	SDL_Delay(10);
}

GUICHAN_Quit();
SDL_Quit();
?>
